------------------------------------
--	Simple Admin System
--	File: Hooks.lua
--	By Spacetech
--	License: http://creativecommons.org/licenses/by-nc-sa/3.0/
------------------------------------

function SAS.PlayerSaid(ply, Text)
	SAS.LogEvent(ply:Nick()..": "..Text)
	local TextLower = string.Trim(string.lower(Text))
	if(string.sub(TextLower, 1, 1) == SAS.ChatCommandPrefix) then
		local FoundCommand = false
		if(table.Count(SAS.Commands) == 0) then SAS.Nofity(ply, "Their are no commands available") return end
		for k, v in pairs(SAS.Commands) do
			v1Lower = string.lower(v[1])
			local ReplaceCP = string.Replace(TextLower, string.sub(TextLower, 1, 1), "")
			local Replace = string.Replace(ReplaceCP, v1Lower, "")
			local ReplaceTrim = string.Trim(Replace)
			local ReplaceTrimExplode = string.Explode(" ", ReplaceTrim)
			local CheckCommand = string.Explode(" ", ReplaceCP)
			if(string.find(CheckCommand[1], v1Lower) == 1) then
				FoundCommand = true
				if(SAS.ContinueCheck(ply, v[3])) then
					if(v[4] == 0 || (table.Count(ReplaceTrimExplode) >= v[4] && string.len(ReplaceTrimExplode[1]) >= 1)) then
						--Based off of SAT by Conna
						local Work, PCallError = pcall(v[2], ply, ReplaceTrimExplode)
						if(!Work) then
							SAS.NError(tostring(PCallError))
						end
					else
						SAS.Nofity(ply, "You did not specify enough arguments")
					end
				else
					SAS.Nofity(ply, "That command is admin only")
				end
			end
		end
		if(FoundCommand == false) then
			SAS.Nofity(ply, "Command not found")
		end
	end
end

function SAS.PlayerSpawnWeapons(ply)
	ply:StripWeapons()
	for k, v in pairs(SAS.Weapons) do
		ply:Give(v)
	end
end

function SAS.PlayerSpawn(ply)
	ply:SetCollisionGroup(11)
	if(SAS.AdminCheck(ply, false) == false && table.Count(SAS.Weapons) != 0) then
		timer.Simple(0.05, SAS.PlayerSpawnWeapons, ply)
	end	
end

--Adapted from a SS function
function SAS.PlayerDeath(victim, weapon, killer)
	local KillerObj = ""
	if(killer:IsPlayer()) then
		KillerObj = killer:Nick()
	else
		KillerObj = killer:GetClass()
	end
	if(KillerObj == victim:Nick()) then
		SAS.LogEvent("#"..KillerObj.." has suicided")
	else
		SAS.LogEvent("#"..victim:Nick().." was killed by"..KillerObj)
	end
end

function SAS.PlayerNoclip(ply)
	SAS.LogEvent("#"..ply:Nick().." has toggled noclip")
end

function SAS.PlayerInitialSpawn(ply)
	if(SAS.AdminCheck(ply, false)) then	
		SAS.Nofity(ply, "ST Admin: Access has been set")
    end
	SAS.Nofity(ply, "Welcome to "..GetConVarString("hostname"))
	SAS.LogEvent("#"..ply:SteamID().."-"..ply:Nick().." has connected")
end

function SAS.PlayerDisconnected(ply)
	SAS.CGod[ply:SteamID()] = false
	SAS.Frozen[ply:SteamID()] = false
	SAS.LogEvent("#"..ply:SteamID().."-"..ply:Nick().." has disconnected")
end

function SAS.PlayerSpawnedEffectProp(ply, mdl, ent)
	SAS.LogEvent("#"..ply:Nick().." has spawned "..mdl)
end

function SAS.PlayerSpawnedSENTVehicleNPC(ply, ent)
	SAS.LogEvent("#"..ply:Nick().." has spawned "..ent:GetModel())
end

function SAS.PlayerSpawnSomething(ply)
	if(SAS.Frozen[ply:SteamID()] == true) then
		return false
	end
end

hook.Add("PlayerSay", "SAS.PlayerSaid", SAS.PlayerSaid)
hook.Add("PlayerSpawn", "SAS.PlayerSpawn", SAS.PlayerSpawn)
hook.Add("PlayerDeath", "SAS.PlayerDeath", SAS.PlayerDeath)
hook.Add("PlayerNoclip", "SAS.DisableNoclip", SAS.PlayerNoclip)
hook.Add("PlayerInitialSpawn", "SAS.PlayerInitialSpawn", SAS.PlayerInitialSpawn)
hook.Add("PlayerDisconnected", "SAS.PlayerDisconnected", SAS.PlayerDisconnected)

hook.Add("PlayerSpawnProp", "SAS.PlayerSpawnProp", SAS.PlayerSpawnSomething)
hook.Add("PlayerSpawnNPC", "SAS.PlayerSpawnNPC", SAS.PlayerSpawnSomething)
hook.Add("PlayerSpawnEffect", "SAS.PlayerSpawnEffect", SAS.PlayerSpawnSomething)
hook.Add("PlayerSpawnVehicle", "SAS.PlayerSpawnVehicle ", SAS.PlayerSpawnSomething)
hook.Add("PlayerSpawnSENT", "SAS.PlayerSpawnSENT", SAS.PlayerSpawnSomething)
hook.Add("PlayerSpawnRagdoll", "SAS.PlayerSpawnRagdoll", SAS.PlayerSpawnSomething)

hook.Add("PlayerSpawnedProp", "SAS.PlayerSpawnProp", SAS.PlayerSpawnedEffectProp)
hook.Add("PlayerSpawnedEffect", "SAS.PlayerSpawnedEffect", SAS.PlayerSpawnedEffectProp)
hook.Add("PlayerSpawnedNPC", "SAS.PlayerSpawnedNPC", SAS.PlayerSpawnedSENTVehicleNPC)
hook.Add("PlayerSpawnedSENT", "SAS.PlayerSpawnedSENT", SAS.PlayerSpawnedSENTVehicleNPC)
hook.Add("PlayerSpawnedVehicle", "SAS.PlayerSpawnedVehicle", SAS.PlayerSpawnedSENTVehicleNPC)